package com.yy.bootdemo.common.utils;


import com.yy.bootdemo.admin.dto.MenDto;
import com.yy.bootdemo.admin.entity.MyMenu;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MenuTree1 {


    private  List<MenDto> menDtoList;

    public List<MenDto> getTree(List<MyMenu> list){

        MenDto menu2=new MenDto();
        menu2.setMenuId(0);
        menDtoList = new ArrayList<MenDto>();
        getTree( menu2,  list);
        return menDtoList;
    }
    public  void  getTree(MenDto menu2, List<MyMenu> nodeList){

        for (MyMenu layer : nodeList) {

            if(menu2.getMenuId().toString().equals(layer.getParentId().toString())){
                MenDto menu=new MenDto();
                menu.setMenuId(layer.getMenuId());
                menu.setParentId(layer.getParentId());
                menu.setMenuName(layer.getMenuName());
                menu.setIcon(layer.getIcon());
                menu.setType(layer.getType());
                menu.setUrl(layer.getUrl());
                menu.setSort(layer.getSort());
                if (menu2.getMenuId().toString().equals("0")){
                    menDtoList.add(menu);
                }else {
                    if (menu2.getChildren()==null){
                        List<MenDto> li=new ArrayList<MenDto>();
                        menu2.setChildren(li);
                    }
                    menu2.getChildren().add(menu);
                }

                getTree(menu,nodeList);
            }
        }


    }

    public  List<Map<String,Object>> tree2(Integer pid, List<MyMenu> nodeList){
        List<Map<String,Object>> resultList =getNodeX(pid,nodeList);
        for (Map<String,Object> menu:resultList ){
            getNode2(menu,nodeList);
        }

        return resultList;
    }
    public  List<Map<String,Object>> getNodeX(Integer pid, List<MyMenu> nodeList){
        List<Map<String,Object>> resultList = new ArrayList<>();

        for (MyMenu layer : nodeList) {
            if(pid.equals(layer.getParentId())){
                Map<String,Object> menu=new HashMap<>();
                menu.put("value",layer.getMenuId());
                menu.put("label",layer.getMenuName());
                menu.put("parentId",layer.getParentId());
                resultList.add(menu);

            }
        }
        return resultList;
    }

    public  void  getNode2(Map<String,Object> menu2, List<MyMenu> nodeList){
        List<Map<String,Object>> resultList = new ArrayList<>();
        for (MyMenu layer : nodeList) {
            if(menu2.get("value").toString().equals(layer.getParentId().toString())){
                Map<String,Object> menu=new HashMap<>();
                menu.put("value",layer.getMenuId());
                menu.put("label",layer.getMenuName());
                menu.put("parentId",layer.getParentId());
                resultList.add(menu);
                getNode2(menu,nodeList);
            }
        }
        if (resultList.size()>0){
            menu2.put("children",resultList);
        }
    }

}
